#include <array>        // 测试部分
#include <iostream>
#include <ctime>        // 用于计算时间
#include <cstdlib>      // qsort bsearch NULL

#include "test.h"       // 用于测试

using std::cin;
using std::cout;
using std::endl;
using std::array;

const long ASIZE  =   500000L; // 测试容量

void test_array() {
    cout << endl
        << "test array now ..." 
        << endl;

    // 定义一个array
    array<long, ASIZE> arr;

    // 开始时间
    clock_t timeStart = clock();

    for (long i{0}; i<ASIZE; ++i) {
        arr[i] = rand();
        // 赋予随机数
    }

    cout << "insert data use : " << transformClockSeconds(clock() - timeStart) << " seconds" << endl;
    // 获取长度
    cout << "array.size() = " << arr.size() << endl;
    // 获取首元素
    cout << "array.front() = " << arr.front() << endl;
    // 获取尾元素
    cout << "array.back() = " << arr.back() << endl;
    // 获取首元素地址:     Get pointer to data (public member function )
    cout << "array.data() = " << arr.data() << endl;

    // 排序
    long target = get_a_target_long(); // 查找目标
    
    timeStart = clock();
    // 快速排序: num*log2(num)
    qsort(arr.data(), ASIZE, sizeof(long), compareLongs);
    cout << "quicksort use time: " << transformClockSeconds(clock() - timeStart) << " seconds" << endl;
    // 二分查找
    // 需要先排序再查找，前者已经排序
    timeStart = clock();
    long *pItem = (long*)bsearch(&target, (arr.data()), ASIZE, sizeof(long), compareLongs);
    cout << "bsearch use time: " << transformClockSeconds(clock() - timeStart) << " seconds" << endl;

    if (pItem != nullptr) {
        cout << "found, " << *pItem << endl;
    } else {
        cout << "sorry, not found" << endl;
    }
}

int main() {
    test_array();

    return 0;
}
